<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/9/24
 * Time: 10:22
 */

namespace app\index\controller;


use app\service\DepositService;
use Redis\Redis;
use think\App;
use think\Controller;
use think\Db;
use think\facade\Request;

class Common extends Controller
{
    # 商户id
    public $bizId;
    # 登录类型
    public $loginType;
    # 初始化redis
    public $_Redis;
    # 员工id
    public $employeeId;
    # 权限
    public $authority;

    /**
     * Common constructor.
     * @param App|null $app
     * @throws \BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function __construct(App $app = null)
    {
        parent::__construct($app);
        header('Access-Control-Allow-Origin:*');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        header('Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers,token');  //响应头 请按照自己需求添加。
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }
        # 员工自动转正
        TimeTask::autoFormal();
        #  商户id
        $this->bizId = Request::header('cooId');
        # 支付缓存状态查询
        TimeTask::payRedis($this->bizId);
        # 员工di
        $this->employeeId = Request::header('employeeId');
        # 登录类型 (1->老板登录  2->员工登录)
        $this->loginType = Request::header('loginType');
        # 初始化redis
        $this->_Redis = new Redis();
        # 老板权限
        $this->authority = [1, 2, 3, 4, 5, 6];
        # 判断门店状态
        if (!empty($this->bizId)) {
            $bizStatus = Db::table('biz')->field('biz_status,recommend_status,expair_time')->where(array('id' => $this->bizId))->find();
            if (!empty($bizStatus)) {
                if ($bizStatus['recommend_status'] == 1) {
                    if (!empty($bizStatus['expair_time']) and date('Y-m-d') > $bizStatus['expair_time']) {
                        Db::table('biz')->where(array('id' => $this->bizId))->update(array('recommend_status' => 2));
                    }
                }
            }
            if (empty($bizStatus) or $bizStatus['biz_status'] != 1) {
                throw new \BaseException(['msg' => '门店不存在或已下架', 'noAttendance' => 1, 'status' => false, 'debug' => false]);
            }
        }
        if ($this->loginType == 2) {
            # 判断该员工今天是否出勤
            $attendance = Db::table('log_attendance')->field('id')
                ->where("date(create_time) = '" . date("Y-m-d") . "'")
                ->where(array('employee_id' => $this->employeeId))
                ->find();
            if (!empty($attendance)) {
                throw new \BaseException(['msg' => '该员工今天未出勤', 'noAttendance' => 1, 'status' => false, 'debug' => false]);
            }
            # 判断员工状态
            $employeeStatus = Db::table('employee')->field('employee_status')->where(array('id' => $this->employeeId))->value('employee_status');
            if ($employeeStatus == 0) {
                throw new \BaseException(['msg' => '已离职', 'noAttendance' => 1, 'status' => false, 'debug' => false]);
            }
            # 员工权限
            $employeeAuthority = $this->_Redis->hGet('employeeAuthority', $this->employeeId);
            if (empty($employeeAuthority)) {
                $employeeAuthority = Db::table('employee')->field('wx_authority')->where(array('id' => $this->employeeId))->value('wx_authority');
                $this->_Redis->hSet('employeeAuthority', $this->employeeId, $employeeAuthority);
            }
            $this->authority = explode(',', $employeeAuthority);
        }
        # 结算定时任务
        TimeTask::settlementTask($this->bizId);
        # 提现列表
        DepositService::depositting();
    }
}
